package com.xaomn.common.config;

import com.xaomn.mapper.SysUserMapper;
import com.xaomn.model.SysUserPo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @author xaoMn
 * @title: YJSRealm
 * @projectName yjs
 * @description: TODO
 * @date 日期：2022/1/25 时间：23:23
 */
@Configuration
public class YjsRealm extends AuthorizingRealm {
    private static final Logger log = LogManager.getFormatterLogger();

    @Resource
    private SysUserMapper sysUserMapper;

    // 认证
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        log.info("进入了认证方法。。。");
        // 获得当前用户
        UsernamePasswordToken userToken = (UsernamePasswordToken) token;
        String username = userToken.getUsername();
        SysUserPo sysUserPo = sysUserMapper.findUserByLoginName(username);
        if (null == sysUserPo) {
            return null;
        }
        // 获得数据库中的用户 和当前用户进行比对 认证
        return new SimpleAuthenticationInfo(sysUserPo, sysUserPo.getPassword(), YjsRealm.class.getSimpleName());
    }

    // 授权
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        log.info("进入了授权方法。。。");
        return null;
    }
}
